package com.feilong.commons.core.lang;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 线程解析
 * 
 * @author <a href="mailto:venusdrogon@163.com">金鑫</a>
 * @version 1.0 Dec 17, 2011 2:00:35 AM
 */
public class ThreadUtil{

	@SuppressWarnings("unused")
	private static final Logger	log	= LoggerFactory.getLogger(ThreadUtil.class);

	/**
	 * 传入currentThread 解析其当前方法的名称
	 * 
	 * @param currentThread
	 *            Thread thread = Thread.currentThread();
	 * @return
	 */
	public static String getCurrentMethodName(Thread currentThread){
		return getMethodName(currentThread, 2);
	}

	/**
	 * 解析其调用者方法的名称
	 * 
	 * @param currentThread
	 *            Thread thread = Thread.currentThread();
	 * @return
	 */
	public static String getCallerMethodName(Thread currentThread){
		return getMethodName(currentThread, 1);
	}

	/**
	 * 传入currentThread 解析调用方法的名称
	 * 
	 * @param currentThread
	 *            currentThread
	 * @param index
	 *            index 索引
	 * @return
	 */
	private static String getMethodName(Thread currentThread,int index){
		StackTraceElement[] stackTraceElements = currentThread.getStackTrace();
		StackTraceElement stackTraceElement = stackTraceElements[index];
		String methodName = stackTraceElement.getMethodName();
		return methodName;
	}
}
